package lxt.service.impl;

import java.util.ArrayList;
import java.util.List;

import lxt.dao.CostFixDao;
import lxt.dao.TruckDao;
import lxt.entity.Parameter;
import lxt.entity.TruckInfo;
import lxt.service.TruckService;
/**
 * 车辆管理service
 * @author Lit
 *
 */
public class TruckServiceImpl implements TruckService {
	private TruckDao truckDao;
	private CostFixDao costFixDao;

	/**
	 * 添加
	 */
	public boolean addTruck(TruckInfo truck)throws Exception {
		this.truckDao.save(truck);
		return true;
	}

	/**
	 * 删除
	 */
	public boolean deleteTruck(String truckId) throws Exception{
		String[] delId = truckId.split(",");
		for(int i=0;i<delId.length;i++){
			String id = delId[i];
			this.truckDao.delete(TruckInfo.class, id);
		}
		return true;
	}

	/**
	 * 查询总数
	 */
	public int loadAllTruckCount(String truckId, String netryTime) throws Exception{
		return this.truckDao.loadAllTruckCount(truckId, netryTime);
	}

	/**
	 * 加载
	 */
	public List<TruckInfo> loadTruck(String truckId, String netryTime,int offset, int length)throws Exception {
		//对关联查询的List进行处理
		List<Object> objects = this.truckDao.loadAllTruck(truckId, netryTime, offset, length);
		//定义要传出的List
		List<TruckInfo> trucks = new ArrayList<TruckInfo>();
		if(objects.size()>0){
			for(int i=0;i<objects.size();i++){
				Object[] ob = (Object[]) objects.get(i);
				TruckInfo truck = (TruckInfo) ob[0];
				Parameter parame = (Parameter) ob[1];
				truck.setTruckType(parame.getParamValue());
				/**
				 * 添加车辆维修次数
				 */
				String s = truck.getTruckId();
				System.out.println(s);
				int num = this.costFixDao.queryFixNum(truck.getTruckId());
				truck.setFixNum(num);
				trucks.add(truck);
			}
		}
		return trucks;
	}

	/**
	 * 根据id加载
	 */
	public TruckInfo loadTruckById(String truckId)throws Exception {
		return (TruckInfo) this.truckDao.get(TruckInfo.class, truckId);
	}
	
	/**
	 * 修改
	 */
	public boolean updateTruck(TruckInfo truck) throws Exception{
		 this.truckDao.update(truck);
		 return true;
	}
	/**
	 * 查询车辆信息
	 */
	@SuppressWarnings("unchecked")
	public List<TruckInfo> loadAll()throws Exception{
		return (List<TruckInfo>) this.truckDao.loadAll(TruckInfo.class);
	}
	
	/**
	 * get set
	 * @return
	 */
	public TruckDao getTruckDao() {
		return truckDao;
	}
	public void setTruckDao(TruckDao truckDao) {
		this.truckDao = truckDao;
	}
	public CostFixDao getCostFixDao() {
		return costFixDao;
	}
	public void setCostFixDao(CostFixDao costFixDao) {
		this.costFixDao = costFixDao;
	}
}
